package com.library.weidget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * Created by wdl on 2016/1/19.
 * 该framelayout 主要是将touch事件进行拦截，交由程序处理
 */
public class CustomeLinearLayout extends LinearLayout {
    public static final int TURN_TOP=0;
    public static final int TURN_BOTTOM=1;

    public interface SlidingListener{
        void silding(int dri);
    }

    public CustomeLinearLayout(Context context) {
        super(context);
    }

    public CustomeLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private SlidingListener listener;

    public void setListener(SlidingListener listener) {
        this.listener = listener;
    }

    private int lastY=-1;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction()==MotionEvent.ACTION_DOWN){
            lastY= (int) ev.getY();
        }
        if (ev.getAction()==MotionEvent.ACTION_MOVE){
            float v = ev.getY() - lastY;
            if (Math.abs(v)>15){
                if (listener!=null){
                    if (v>0)
                        listener.silding(TURN_BOTTOM);
                    else
                        listener.silding(TURN_TOP);
                }
                return true;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }
}
